<?php

namespace App\Shop\Controllers;

use App\Admin\Extensions\Table\ShopSelectTable;
use App\Admin\Repositories\Service;
use App\Models\Area;
use App\Models\CarBrand;
use App\Models\ServiceCategory;
use App\Models\ShopUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Validation\Rule;

class ServiceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Service(['category']), function (Grid $grid) {
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id);
            $grid->column('id')->sortable();
            $grid->column('category.name','所属分类');
            $grid->column('image')->image(null,50,50);
            $grid->column('title');
            $grid->column('subtitle');
            $grid->column('price');
            $grid->column('sort')->sortable();
            $grid->column('status')->radio(['正常','隐藏'],true)->label(['info','default']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) use ($shop_id){
                $filter->panel()->expand(false);
                $filter->equal("id")->width(2);
                $filter->equal("cid","所属分类")->select(ServiceCategory::getOptions($shop_id))->width(2);
                $filter->like("title")->width(2);
                $filter->like("subtitle")->width(3);
                $filter->between("price")->width(3);
                $filter->equal("status")->radio(['正常','隐藏'])->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Service(), function (Show $show) {
            $show->field('id');
            $show->field('image');
            $show->field('title');
            $show->field('subtitle');
            $show->field('price');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        admin_style(<<<CSS
.tab-pane:nth-child(2)>.row>.col-md-6>.row>.col-md-8{
    display: none !important;
}
.tab-pane:nth-child(2)>.row>.col-md-6>.row>.col-md-2{
    flex: 0 0 45% !important;
    max-width: 45% !important;
}
.tab-pane:nth-child(3)>.row>.col-md-2{
    display: none !important;
}
.tab-pane:nth-child(3)>.row>.col-md-8{
    flex: 0 0 100% !important;
    max-width: 100% !important;
}
CSS);
        return Form::make(new Service(), function (Form $form) {
            $form->tab("基础信息",function (Form $form){
                $form->column(5,function (Form $form){
                    $admin = Admin::user();
                    $isShop = $admin->isAdministrator();
                    if ($isShop){
                        $shop_id = $admin->id;
                    }else{
                        $shop_id = $admin->shop_id;
                    }
                    $form->image('image')->uniqueName()->autoUpload()->help("图片尺寸:118*90")->required();
                    $form->select('cid','所属分类')->options(ServiceCategory::getOptions($shop_id))->width(5)->required();
                    $form->number('sort')->default(0);
                    $form->radio('status')->options(['正常','隐藏'])->default(0);
                });
                $form->column(7,function (Form $form){
                    $id = $form->getKey();
                    $message = ['unique' => '此服务项目已存在' ];
                    $rules = [
                        'required'
                    ];
                    $shopUser = auth("shop")->user();
                    $shop_id = $shopUser->id;
                    $form->hidden('shop_id')->default($shop_id)->value($shop_id);
                    if($id){
                        $rules[] = Rule::unique('services','title')->where("shop_id",$shop_id)->ignore($id);
                    }else{
                        $rules[] = Rule::unique('services','title')->where("shop_id",$shop_id);
                    }
                    $form->text('title')->rules($rules,$message)->required();
                    $form->text('subtitle');
                    $typeList = CarBrand::getTypesList();
                    foreach ($typeList as $key=>$type){
                        $form->decimal("prices.".$key,$type."价格")->required();
                        $form->decimal("member_prices.".$key,$type."会员价格")->required();
                    }
                });
            });
            $form->tab("洗车工分成设置",function (Form $form){
                $typeList = CarBrand::getTypesList();
                foreach ($typeList as $key=>$type){
                    $form->decimal("worker_rates.".$key,$type."价格对应的洗车工佣金")->required();
                }
            });
            $form->tab("图文介绍",function (Form $form){
                $form->ckeditor("content");
            });
            $form->saving(function (Form $form){
                if ($form->isCreating()||$form->isEditing()){
                    $areas = request()->input("areas");
                    $form->model()->areas = $areas?json_decode($areas,true):[];
                }
            });
        });
    }
}
